/**
 * fileName:  FruitMasterReader.java
 * author:  huaimi
 * Company:  Cloudland Studio
 * Createdate:  2014-6-7 下午1:24:38
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-7          huaimi            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.read;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.store.DatabaseReader;
import org.cloudland.commercial.masterdata.store.entity.FruitEntity;
import org.cloudland.commercial.masterdata.store.entity.PageQueryEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.MasterDataEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.MasterDataMapper;
import org.cloudland.jdbc.ibatis.masterdata.handler.condition.FruitTerm;
import org.springframework.stereotype.Component;

 /**
 * 水果仓储读取实现类，使用数据源的读模板来查询数据
 * @ClassName  FruitMasterReader
 * @author  huaimi
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
@Component
public class FruitMasterReader implements DatabaseReader<FruitEntity> 
{
	@Resource(name="masterDataMapper")
	private MasterDataMapper fruitMapper;
	
	/**
	 * <pre>
	 * 按条件查询返回结果
	 * </pre>
	 *
	 * @param term
	 * @return   
	 */
	private PageQueryEntity<FruitEntity> findFruitByTerm(FruitTerm term) {
		List<MasterDataEntity> listDao = fruitMapper.findFruitInfoByTerm(term);
		List<FruitEntity> result = new ArrayList<FruitEntity>(listDao.size());
		FruitEntity entity = null;
		for (MasterDataEntity daoEntity : listDao) {
			entity = new FruitEntity();
			entity.setFruitId(daoEntity.getFruitId());
			entity.setFruitName(daoEntity.getFruitName());
			entity.setFruitAliasName(daoEntity.getFruitAliasName());
			entity.setFruitPinyin(daoEntity.getFruitPinyin());
			entity.setDescription(daoEntity.getDescription());
			entity.setCountryId(daoEntity.getCountryId());
			entity.setRegionId(daoEntity.getRegionId());
			entity.setTaboo(daoEntity.getTaboo());
			entity.setFruitCategoryId(daoEntity.getFruitCategoryId());
			
			result.add(entity);
		}
		
		int count = fruitMapper.findCountByTerm(term);
		
		return new PageQueryEntity<FruitEntity>(count, result);
	}
	
	/**
	 * 
	 * 按主键编号获取水果信息
	 *
	 * @param id 水果主键编号
	 * @return 水果对象
	 */
	public FruitEntity queryById(String id)
	{
		FruitEntity entity = new FruitEntity();
		MasterDataEntity daoEntity = fruitMapper.queryMasterDataByFruitId(id);
		if (daoEntity != null)
		{
			entity.setFruitId(daoEntity.getFruitId());
			entity.setFruitName(daoEntity.getFruitName());
			entity.setFruitAliasName(daoEntity.getFruitAliasName());
			entity.setFruitPinyin(daoEntity.getFruitPinyin());
			entity.setDescription(daoEntity.getDescription());
			entity.setCountryId(daoEntity.getCountryId());
			entity.setRegionId(daoEntity.getRegionId());
			entity.setTaboo(daoEntity.getTaboo());
			entity.setFruitCategoryId(daoEntity.getFruitCategoryId());
		}
		
		return entity;
	}
	
	/**
	 * 
	 *  按分类编号获取水果信息集合
	 * @param categoryId 分类编号
	 * @return 水果信息集合
	 */
	public PageQueryEntity<FruitEntity> queryByCategory(String categoryId, int beginRow, int pageSize)
	{
		FruitTerm term = new FruitTerm();
		term.setBeginRow(beginRow);
		term.setPageRow(pageSize);
		term.setCategoryId(categoryId);
		
		return findFruitByTerm(term);
	}
	
	/**
	 * 
	 * 分页查询水果信息
	 * @param beginRow 分页查询起始记录数
	 * @param pageSize 分页查询每页显示记录数
	 * @return 水果分页的记录集合
	 */
	public PageQueryEntity<FruitEntity> queryByPage(int beginRow, int pageSize)
	{
		FruitTerm term = new FruitTerm();
		term.setBeginRow(beginRow);
		term.setPageRow(pageSize);
		
		return findFruitByTerm(term);
	}
	
	public PageQueryEntity<FruitEntity> searchByPage(String type,String str, int beginRow, int pageSize)
	{
		FruitTerm term = new FruitTerm();
		if(type.equals("2")){
			term.setName(str);
		}else if(type.equals("3")){
			term.setCategoryId(str);
		}
		term.setBeginRow(beginRow);
		term.setPageRow(pageSize);
		
		return findFruitByTerm(term);
	}
	
}
